﻿using System;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using log4net;

using GeoRef;
using Security;
using System.Web;

namespace TripLOA.GUI.secure
{
    public partial class createNewTrip : TripLOA.GUI.internationalization.LOABasePage
    {
#if LOG
        private readonly ILog _logger = LogManager.GetLogger(typeof(createNewTrip));
#endif
        #region Event handlers
        //select index drop down veicoli
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {


            List<Storage.EntityModels.Users_VehicleDetails> vehicles = null;
            if (Session["vehicle"] != null) vehicles = (List<Storage.EntityModels.Users_VehicleDetails>)Session["vehicle"];
            else
            {
                TripLoaUser us = Security.TripLoaUserManager.Instance.getUser(User.Identity.Name);
                vehicles = us.GetProfile().UsersVehiclesDetails;
            }
            ExampleControl_1.Val = vehicles[ddlvehicle.SelectedIndex].VehicleDetails.VehicleClass;
            controllo.Update();  
        }
       


        protected void Page_Load(object sender, EventArgs e)
        {

            List<Storage.EntityModels.Users_VehicleDetails> vehicles=null;

              map_1.GoogleApiKey = System.Configuration.ConfigurationManager.AppSettings["googlekey"];
            if (!IsPostBack)
            {

                TripLoaUser us = Security.TripLoaUserManager.Instance.getUser(User.Identity.Name);
                vehicles = us.GetProfile().UsersVehiclesDetails;
                ListItem item =null;
                for (int i = 0; i < vehicles.Count; i++)
                {
                    String internationalvehicle =(String) HttpContext.GetLocalResourceObject("~/secure/createNewTrip.aspx", vehicles[i].VehicleDetails.Model);
                    
                    if (internationalvehicle != null)
                    {
                        item=new ListItem(internationalvehicle,vehicles[i].Id.ToString());
                        vehicles[i].VehicleDetails.Model = internationalvehicle;
                    }
                    else
                    {
                        item=new ListItem(vehicles[i].VehicleDetails.Model, vehicles[i].Id.ToString());
                    }
                    ddlvehicle.Items.Add(item);
                }
                map_1.Vehicles = vehicles;
               
                map_1.SetHelp((String)HttpContext.GetGlobalResourceObject("TripLoaResource", "HelpEdit")); 
                Session["vehicle"] = vehicles;
                ddlvehicle.SelectedIndex = 8;
                ExampleControl_1.Val = Storage.EntityModels.VehicleTypes.Car;
                controllo.Update();
            }
            else
            {
                if (Session["vehicle"] != null) vehicles = (List<Storage.EntityModels.Users_VehicleDetails>)Session["vehicle"];
                else
                {
                    TripLoaUser us = Security.TripLoaUserManager.Instance.getUser(User.Identity.Name);
                    vehicles = us.GetProfile().UsersVehiclesDetails;
                }
                ExampleControl_1.Val = vehicles[ddlvehicle.SelectedIndex].VehicleDetails.VehicleClass;
            }
            map_1.InvokeJSCommand("this._stuff.cboName = 'ctl00$ContentHomePage$ddlvehicle';");
        }

        protected void BtnCreate_Click(object sender, EventArgs e)
        {
#if LOG
            _logger.Debug("INIZIO CREAZIONE VIAGGIO");
#endif
            TripLoaUser us = Security.TripLoaUserManager.Instance.getUser(User.Identity.Name);
            Groupware.Trip grtrip = new Groupware.Trip(us.UserID, map_1.EditableTrip);
            bool success = true;
            grtrip.IsFrequent = Frequency.Checked;
            if (!Frequency.Checked)
            {
                if (Draft.Checked)
                {
                    if (!isDraft(grtrip)) success = false;
                }
                else
                {
                    if (!isNotDraft(grtrip)) success = false;
                }
            }
            else
            {
               
                if (Draft.Checked)
                {
                    bool current = ExampleControl_1.save(grtrip, Draft.Checked);
                    if (!current) success = false;
                    success=ValidateandSetDraftFreq(grtrip);
                    if (!success) UpdatePanel1.Update();

                    
                }
                else
                {
                    success=ValidateandSetNotDraftFreq(grtrip);
                    bool current = ExampleControl_1.save(grtrip, Draft.Checked);
                    if (!current) success = false;
                    if (!success) UpdatePanel1.Update();

                    
                }
            }
            if (success)
            {
                grtrip.Draft = Draft.Checked;
                
                SaveorError(grtrip);
            }
#if LOG
            _logger.Debug("FINE CREAZIONE VIAGGIO");
#endif
        }

        protected void BtnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/secure/Home.aspx", false);
        } 

        #endregion

        #region Private methods
        protected bool ValidateandSetDraftFreq(Groupware.Trip grtrip)
        {
            bool depardatebool = false;
            bool arrivedatebool = false;
            bool successvalidation = true;
            DateTime start = new DateTime();
            DateTime stop = new DateTime();
            if (!DepartureDate.Text.Equals(String.Empty))
            {
                depardatebool = valiDate(ddValidator, DepartureDate.Text);
                if (!depardatebool) successvalidation = false;
            }
            if (!ArriveDate.Text.Equals(String.Empty))
            {
                arrivedatebool = valiDate(adValidator, ArriveDate.Text);
                if (!arrivedatebool) successvalidation = false;
            }
            if (depardatebool && arrivedatebool)
            {
                start = DateTime.Parse(DepartureDate.Text);
                stop = DateTime.Parse(ArriveDate.Text);

                if (stop.Date <= start.Date)
                {
                    successvalidation = false;
                    visible(ddValidator);
                    visible(adValidator);
                }
            }
            if (!ptc.IsValid)
            {
                periodicvalidator.Text = ptc.ErrorMessage;
                successvalidation = false;
                visible(periodicvalidator);
            }
            else hide(periodicvalidator);

            if (successvalidation)
            {
                grtrip.EstimatedTime = (short)ptc.TripDuration.TotalMinutes;
            }
            if (successvalidation && depardatebool && !arrivedatebool)
            {
                if (ptc.ScheduledDaysNumber > 0)
                {
                    DateTime firstdeparture = getHourFromFrequencyControl(start);
                    grtrip.DepartureDate = firstdeparture;
                    grtrip.ReservationExpirationDate = firstdeparture.Subtract(new TimeSpan(1, 0, 0));
                }
                else
                {
                    grtrip.DepartureDate = start;
                    grtrip.ReservationExpirationDate = start.Subtract(new TimeSpan(1, 0, 0));
                }
            }
           
            if (successvalidation && depardatebool && arrivedatebool)
            {
                if (ptc.ScheduledDaysNumber > 0)
                {

                    DateTime firstdeparture = getHourFromFrequencyControl(start);
                    if (!((firstdeparture >= start) && (firstdeparture < stop)))
                    {
                        periodicvalidator.Text = (String)HttpContext.GetLocalResourceObject("~/secure/createNewTrip.aspx", "frequencyerror");
                        visible(periodicvalidator);
                        return false;
                    }
                    grtrip.DepartureDate = firstdeparture;
                    grtrip.ReservationExpirationDate = firstdeparture.Subtract(new TimeSpan(1, 0, 0));
                }
                else
                {
                    grtrip.DepartureDate = start;
                    grtrip.ReservationExpirationDate =start.Subtract(new TimeSpan(1, 0, 0));
                }
                if(successvalidation)grtrip.SetFrequencyDetails(start, stop, ptc.DeparturesTime);
            }
            return successvalidation;


        }
        protected DateTime getHourFromFrequencyControl(DateTime dt)
        {
            DayOfWeek current = dt.DayOfWeek;
            DateTime currentdate = dt;


            String[] getsethour = ptc.DeparturesTime;
            for (int i = 0; i < 7; i++)
            {
                current = dt.DayOfWeek;
                switch (current)
                {
                    case DayOfWeek.Sunday:
                        {

                            if (!getsethour[0].Equals("DayNotScheduled"))
                            {
                                DateTime found = DateTime.Parse(dt.Day + " " + dt.Month + " " + dt.Year + " " + getsethour[0]);
                                return found;
                            }
                            break;
                        }
                    case DayOfWeek.Monday:
                        {
                            if (!getsethour[1].Equals("DayNotScheduled"))
                            {
                                DateTime found = DateTime.Parse(dt.Day + " " + dt.Month + " " + dt.Year + " " + getsethour[1]);
                                return found;
                            }
                            break;
                        }
                    case DayOfWeek.Thursday:
                        {
                            if (!getsethour[2].Equals("DayNotScheduled"))
                            {
                                DateTime found = DateTime.Parse(dt.Day + " " + dt.Month + " " + dt.Year + " " + getsethour[2]);
                                return found;
                            }
                            break;
                        }
                    case DayOfWeek.Wednesday:
                        {
                            if (!getsethour[3].Equals("DayNotScheduled"))
                            {
                                DateTime found = DateTime.Parse(dt.Day + " " + dt.Month + " " + dt.Year + " " + getsethour[3]);
                                return found;
                            }
                            break;
                        }
                    case DayOfWeek.Tuesday:
                        {
                            if (!getsethour[4].Equals("DayNotScheduled"))
                            {
                                DateTime found = DateTime.Parse(dt.Day + " " + dt.Month + " " + dt.Year + " " + getsethour[4]);
                                return found;
                            }
                            break;
                        }
                    case DayOfWeek.Friday:
                        {
                            if (!getsethour[5].Equals("DayNotScheduled"))
                            {
                                DateTime found = DateTime.Parse(dt.Day + " " + dt.Month + " " + dt.Year + " " + getsethour[5]);
                                return found;
                            }
                            break;
                        }
                    case DayOfWeek.Saturday:
                        {
                            if (!getsethour[6].Equals("DayNotScheduled"))
                            {
                                DateTime found = DateTime.Parse(dt.Day + " " + dt.Month + " " + dt.Year + " " + getsethour[6]);
                                return found;
                            }
                            break;
                        }
                }
                dt = dt.AddDays(1);



            }
            return currentdate;
        }
        protected bool ValidateandSetNotDraftFreq(Groupware.Trip grtrip)
        {

            bool successvalidation = true;
            DateTime start = new DateTime();
            DateTime stop = new DateTime();
            if (DepartureDate.Text.Equals(String.Empty))
            {
                successvalidation = false;
                visible(ddValidator);
            }
            else
            {
                if (!valiDate(ddValidator, DepartureDate.Text)) successvalidation = false;
            }
            if (ArriveDate.Text.Equals(String.Empty))
            {
                visible(adValidator);
                successvalidation = false;
            }
            else
            {
                if (!valiDate(adValidator, ArriveDate.Text)) successvalidation = false;

            }
            if (successvalidation)
            {
                start = DateTime.Parse(DepartureDate.Text);
                stop = DateTime.Parse(ArriveDate.Text);
                if (stop.Date <= start.Date)
                {
                    successvalidation = false;
                    visible(ddValidator);
                    visible(adValidator);
                }

            }
            if (!ptc.IsValid)
            {
                periodicvalidator.Text = ptc.ErrorMessage;
                visible(periodicvalidator);
                successvalidation = false;
            }
            if (ptc.ScheduledDaysNumber == 0)
            {
                successvalidation = false;
                periodicvalidator.Text = (String)HttpContext.GetLocalResourceObject("~/secure/createNewTrip.aspx", "frequencyerror");
                visible(periodicvalidator);
            }
            if (ptc.IsValid && ptc.ScheduledDaysNumber > 0) hide(periodicvalidator);
            if (successvalidation)
            {
                DateTime firstdeparture=getHourFromFrequencyControl(start);
                if (!((firstdeparture >= start) && (firstdeparture < stop)))
                {
                    periodicvalidator.Text = (String)HttpContext.GetLocalResourceObject("~/secure/createNewTrip.aspx", "frequencyerror");
                    visible(periodicvalidator);
                    return false;
                }
                grtrip.DepartureDate = firstdeparture;
                grtrip.ReservationExpirationDate =firstdeparture.Subtract(new TimeSpan(1,0,0));
                grtrip.EstimatedTime = (short)ptc.TripDuration.TotalMinutes;
                grtrip.SetFrequencyDetails(start, stop, ptc.DeparturesTime);
            }

            return successvalidation;
        } 
        private Dictionary<byte, string> stubData()
        {
            Dictionary<byte, string> result = new Dictionary<byte, string>(7);
            result.Add(0, "DayNotScheduled");
            result.Add(1, "08:00:00");
            result.Add(2, "12:00:00");
            result.Add(3, "DayNotScheduled");
            result.Add(4, "12:00:00");
            result.Add(5, "12:12:00");
            result.Add(6, "DayNotScheduled");
            return result;
        }
        //questo metodo invoca il metodo di groupwere se và a createdTrips vuol dire che il viaggio è stato
        //salvato altrimenti visualizza errori groupware
        private void SaveorError(Groupware.Trip grtrip)
        {
            if (grtrip.DeviationRadius == null) grtrip.DeviationRadius = 0;
            int idtrip = grtrip.SaveTrip();
            if (idtrip > 0)
            {
                Response.Redirect("~/secure/CreatedTrips.aspx", false);
            }
            else
            {
                String error = String.Empty;
                for (int j = 0; j < grtrip.Errors.Count; j++)
                {
                    error =error+"\""+grtrip.Errors[j].fieldName+"\"\n";   
                }
                ScriptManager.RegisterClientScriptBlock(up_button, this.GetType(), "errorfunction", "alert(" +error +");", true);
            }
        }

        private void hide(Label label)
        {
            label.Style[HtmlTextWriterStyle.Visibility] = "hidden";
            label.Style[HtmlTextWriterStyle.FontSize] = "1pt";
        }

        private void visible(Label label)
        {
            label.Style[HtmlTextWriterStyle.Visibility] = "visible";
            label.Style[HtmlTextWriterStyle.FontSize] = "10pt";
        }
        //controlla che sia un'ora
        private bool ishour(int h, int min)
        {
            if ((h >= 0) && (h < 24) && (min >= 0) && (min < 60)) 
                return true;
            else 
                return false;
        }
        //validatore data
        private bool valiDate(Label lab, String text)
        {
            try
            {
                DateTime dt = DateTime.Parse(text);
            }
            catch
            {
                visible(lab);
                return false;                   
            }
            
            hide(lab);
            return true;
        }
        //validatore ora e minuti
        private bool validaTime(Label lab,String text)
        {
            int ora = 0;
            int min = 0;
             
            try
            {
                ora = Convert.ToInt32(text.Substring(0, 2));
                min = Convert.ToInt32(text.Substring(3, 2));
            }
            catch 
            {
                visible(lab);
                return false;
            }
             
            if (!ishour(ora, min))
            {
                visible(lab);
                return false;
            }
            else
            {
                hide(lab);
                return true;
            }
        }
        //controlli incrociati tra data partenza e data arrivo
        //arrivo >partenza
        private bool crossed(Groupware.Trip grt)
        {
             bool success = true;
             DateTime dtdeparture = DateTime.Parse(DepartureDate.Text.Trim() + " " + DepartureTime.Text);
             DateTime dtarrive = DateTime.Parse(ArriveDate.Text + " " + ArriveHour.Text);
             TimeSpan diff = dtarrive.Subtract(dtdeparture);
             DateTime now = DateTime.Now;

             if (diff.TotalMinutes < 0)
             {
                 success = false;

                 if (DepartureDate.Text.Equals(ArriveDate))
                 {
                     visible(dtValidator);
                     visible(atValidator);
                 }
                 else
                 {
                     visible(ddValidator);
                     visible(adValidator);
                 }
             }

             if (now > dtdeparture)
             {
                 success = false;

                 if (now.Date.CompareTo(dtdeparture.Date) == 0)
                 {
                     visible(dtValidator);
                     visible(atValidator);
                 }
                 else
                 {
                     visible(ddValidator);
                     visible(adValidator);
                 }  
             }

             if (now > dtarrive)
             {
                 success = false;

                 if (now.Date.CompareTo(dtdeparture.Date) == 0)
                 {
                     visible(dtValidator);
                     visible(atValidator);
                 }
                 else
                 {
                     visible(ddValidator);
                     visible(adValidator);
                 }
             }

             if (success)
             {
                 grt.EstimatedTime = (short)diff.TotalMinutes;
                 grt.DepartureDate = dtdeparture;
             }

             return success;
        }
        //effettua le validazioni se non è una bozza e setta le proprietà
        private bool isNotDraft(Groupware.Trip grtrip)
        {
            bool successvalidation = true;

            if (DepartureDate.Text.Equals(String.Empty)) 
            {
                successvalidation = false;
                visible(ddValidator);
            }
            else
            {
                if (!valiDate(ddValidator, DepartureDate.Text)) successvalidation = false;
                   
                
            }

            if (DepartureTime.Text.Equals(String.Empty))
            {
                successvalidation = false;
                visible(dtValidator);
            }
            else
            {
                if (!validaTime(dtValidator,DepartureTime.Text)) successvalidation = false;
               
            }
            

            if (ArriveDate.Text.Equals(String.Empty)) 
            {
                visible(adValidator);
                successvalidation = false;
            }
            else
            {
                if (!valiDate(adValidator, ArriveDate.Text)) successvalidation = false;
                
            }

            if (ArriveHour.Text.Equals(String.Empty)) 
            {
                successvalidation = false;
                visible(atValidator);
            }
            else
            {
                if (!validaTime(atValidator, ArriveHour.Text)) successvalidation = false;
                    
                
            }

            if (expdate.Text.Equals(String.Empty))
            {
                visible(expdvalidator);
                successvalidation = false;
            }
            else{

                if (!valiDate(expdvalidator, expdate.Text)) successvalidation = false;
                
            }
            if (exptime.Text.Equals(String.Empty))
            {
                visible(exptvalidator);
                successvalidation = false;
            }
            else{

                if(!validaTime(exptvalidator, exptime.Text)) successvalidation=false;
             }
            if (successvalidation)
            {
                DateTime departure = DateTime.Parse(DepartureDate.Text+" "+DepartureTime.Text);
                DateTime arrive = DateTime.Parse(ArriveDate.Text+" "+ArriveHour.Text);
                DateTime expiration = DateTime.Parse(expdate.Text+" "+exptime.Text);
                if (expiration > departure)
                {
                    visible(expdvalidator);
                    visible(exptvalidator);
                    visible(ddValidator);
                    visible(dtValidator);
                    successvalidation = false;
                }
                if (expiration > arrive)
                {
                    visible(expdvalidator);
                    visible(exptvalidator);
                    visible(adValidator);
                    visible(atValidator);
                    successvalidation = false;
                }
            }


            
            if (successvalidation && !crossed(grtrip)) 
                successvalidation = false;
            
            bool current = ExampleControl_1.save(grtrip, Draft.Checked);
            if (successvalidation) grtrip.ReservationExpirationDate = DateTime.Parse(expdate.Text + " " + exptime.Text);
            if (!current) 
                successvalidation = false;

            UpdatePanel1.Update();
            
            return successvalidation;
        }

        private bool compareDateWithNow(String t)
        {
            DateTime now = DateTime.Now;
            DateTime current = DateTime.Parse(t);
            return (current.Date.CompareTo(now.Date) >= 0);
            
        }

        private bool compareDateTimeWithNow(String t)
        {
            DateTime now = DateTime.Now;
            DateTime current = DateTime.Parse(t);
            return (current > now);

            
        }
        //effettua le validazioni se è una bozza e setta le proprietà
        private bool isDraft(Groupware.Trip grtrip)
        {
            bool depardatebool = false;
            bool departuretimebool = false;
            bool arrivedatebool = false;
            bool arrivetimebool = false;
            bool expdatebool = false;
            bool exptimebool = false;
            bool successvalidation = true;
         
            if (!DepartureDate.Text.Equals(String.Empty))
            {
                depardatebool = valiDate(ddValidator, DepartureDate.Text);
                if (!depardatebool) successvalidation = false;
            }

            if (!DepartureTime.Text.Equals(String.Empty))
            {
                departuretimebool = validaTime(dtValidator, DepartureTime.Text);
                if (!departuretimebool) successvalidation = false;
            }

            if (!ArriveDate.Text.Equals(String.Empty))
            {
                arrivedatebool = valiDate(adValidator, ArriveDate.Text);
                if (!arrivedatebool) successvalidation = false;
            }

            if (!ArriveHour.Text.Equals(String.Empty))
            {
                arrivetimebool =validaTime(atValidator,ArriveHour.Text);
                if (!arrivetimebool) successvalidation = false;
            }

            if (depardatebool && departuretimebool && arrivedatebool && arrivetimebool)
            {
                if (!crossed(grtrip))
                    successvalidation = false;
            }
            else
            {
                if (depardatebool && arrivedatebool)
                {
                    if (DateTime.Parse(DepartureDate.Text).Date.CompareTo(DateTime.Parse(ArriveDate.Text)) > 0)
                    {
                        successvalidation = false;
                        visible(ddValidator);
                        visible(adValidator);
                    }
                }
            }

            if (depardatebool && departuretimebool)
            {
                if (!compareDateTimeWithNow(DepartureDate.Text + " " + DepartureTime.Text))
                {
                    successvalidation = false;
                    visible(ddValidator);
                    visible(dtValidator);
                }
                
                grtrip.DepartureDate = DateTime.Parse(DepartureDate.Text + " " + DepartureTime.Text);
            }
            else
            {
                if (depardatebool && !departuretimebool)
                {
                    if (!compareDateWithNow(DepartureDate.Text))
                    {
                        visible(adValidator);
                        visible(atValidator);
                        successvalidation = false;
                    }

                    grtrip.DepartureDate = DateTime.Parse(DepartureDate.Text);
                }
            }

            if (arrivedatebool && arrivetimebool)
            {
                if (!compareDateTimeWithNow(ArriveDate.Text + " " + ArriveHour.Text))
                {
                    visible(adValidator);
                    visible(atValidator);
                    successvalidation = false;
                }
            }
            else
            {
                if (arrivedatebool && ! arrivetimebool)
                {
                    if (!compareDateWithNow(ArriveDate.Text))
                    {
                        visible(adValidator);
                        successvalidation = false;
                    }
                }
            }
            if (!expdate.Text.Equals(String.Empty))
            {
                expdatebool = valiDate(expdvalidator, expdate.Text);
                if (!expdatebool) successvalidation = false;
            }
            if (!exptime.Text.Equals(String.Empty))
            {
                exptimebool = validaTime(exptvalidator, exptime.Text);
                if (!exptimebool) successvalidation = false;
            }
            if (expdatebool && exptimebool)
            {
                if (depardatebool && departuretimebool)
                {
                    DateTime depdate =DateTime.Parse(DepartureDate.Text + " " + DepartureTime.Text);
                    DateTime expdd = DateTime.Parse(expdate.Text + " " + exptime.Text);
                    if (expdd>depdate)
                    {
                        successvalidation = false;
                        visible(ddValidator);
                        visible(dtValidator);
                        visible(expdvalidator);
                        visible(exptvalidator);
                    }
                }
                else
                {
                    if (depardatebool)
                    {
                        DateTime depdate = DateTime.Parse(DepartureDate.Text);
                        DateTime expdd = DateTime.Parse(expdate.Text);
                        if (expdd.Date.CompareTo(depdate.Date) > 0)
                        {
                            successvalidation = false;
                            visible(ddValidator);
                            visible(expdvalidator);
                        }
                    }
                }
                if (arrivedatebool && arrivetimebool)
                {
                    DateTime arrdate = DateTime.Parse(ArriveDate.Text + " " + ArriveHour.Text);
                    DateTime expdd = DateTime.Parse(expdate.Text + " " + exptime.Text);
                    if (expdd.CompareTo(arrdate) > 0)
                    {
                        successvalidation = false;
                        visible(adValidator);
                        visible(atValidator);
                        visible(expdvalidator);
                        visible(exptvalidator);
                    }
                }
                else
                {
                    if (arrivedatebool)
                    {
                        DateTime arrdate = DateTime.Parse(ArriveDate.Text);
                        DateTime expdd = DateTime.Parse(expdate.Text);
                        if (expdd.Date.CompareTo(arrdate.Date) > 0)
                        {
                            successvalidation = false;
                            visible(adValidator);
                            visible(expdvalidator);
                        }
                    }
                }
            }
            else
            {
                if (expdatebool)
                {
                    if (depardatebool)
                    {
                        DateTime depdate = DateTime.Parse(DepartureDate.Text);
                        DateTime expdd = DateTime.Parse(expdate.Text);
                        if (expdd.Date.CompareTo(depdate.Date) > 0)
                        {
                            successvalidation = false;
                            visible(ddValidator);
                            visible(expdvalidator);
                        }
                    }
                    if (arrivedatebool)
                    {
                        DateTime arrdate = DateTime.Parse(ArriveDate.Text);
                        DateTime expdd = DateTime.Parse(expdate.Text);
                        if (expdd.Date.CompareTo(arrdate.Date) > 0)
                        {
                            successvalidation = false;
                            visible(adValidator);
                            visible(expdvalidator);
                        }
                    }
                }
            }
            
            bool current = ExampleControl_1.save(grtrip,Draft.Checked);
            
            if (!current) successvalidation = false;
            if (successvalidation && expdatebool && !exptimebool) grtrip.ReservationExpirationDate = DateTime.Parse(expdate.Text);
            else
            {
                if (successvalidation && expdatebool && exptimebool) grtrip.ReservationExpirationDate = DateTime.Parse(expdate.Text+" "+exptime.Text);
            }
            UpdatePanel1.Update();
            return successvalidation;
        }

        #endregion
    }
}